package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.ToolsCheckPlanDao;
import com.sugon.dao.ToolsCheckTicketDao;
import com.sugon.entity.ToolsCheckPlanEntity;
import com.sugon.entity.dto.ToolsCheckPlanImplements;
import com.sugon.modules.base.model.vo.ProcessProgressConst;
import com.sugon.service.ToolsCheckPlanService;
import com.sugon.utils.Constant;
import com.sugon.utils.ShiroUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 工装周期性检测计划，以产线为单位Service实现类
 *
 * @author YuXD
 * @date 2021-10-03
 */
@Service("toolsCheckPlanService")
public class ToolsCheckPlanServiceImpl implements ToolsCheckPlanService {

    @Resource
    private ToolsCheckPlanDao toolsCheckPlanDao;
    @Resource
    private ToolsCheckTicketDao toolsCheckTicketDao;

    @Override
    public boolean checkExists(String devLineId, Date startDate, Date endDate) {
        Map<String, Object> queryParam = MapUtil.newHashMap();
        queryParam.put("devLineId", devLineId);
        List<ToolsCheckPlanEntity> checkPlanList = queryList(queryParam);
        if (CollectionUtil.isNotEmpty(checkPlanList)) {
            for (ToolsCheckPlanEntity checkPlan : checkPlanList) {
                if (!(DateUtil.compare(endDate, checkPlan.getStartDate()) < 0 || DateUtil.compare(startDate, checkPlan.getEndDate()) > 0)) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public ToolsCheckPlanEntity findLastestPassedPlan(String planId, String devLineId) {
        Map<String, Object> queryParam = MapUtil.newHashMap();
        queryParam.put("id", planId);
        queryParam.put("devLineId", devLineId);
        queryParam.put("status", ProcessProgressConst.PASSED);
        List<ToolsCheckPlanEntity> checkPlanList = queryList(queryParam);
        return CollectionUtil.isNotEmpty(checkPlanList) ? checkPlanList.get(0) : null;
    }

    /**
     * 查询
     *
     * @param id 主键
     * @return
     */
    @Override
    public ToolsCheckPlanEntity queryObject(String id) {
        return toolsCheckPlanDao.queryObject(id);
    }

    /**
     * 列表查询
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<ToolsCheckPlanEntity> queryList(Map<String, Object> map) {
        return toolsCheckPlanDao.queryList(map);
    }

    /**
     * 列表数查询
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return toolsCheckPlanDao.queryTotal(map);
    }

    /**
     * 保存
     *
     * @param toolsCheckPlan 实体
     * @return
     */
    @Override
    public int save(ToolsCheckPlanEntity toolsCheckPlan) {
        toolsCheckPlan.setId(RandomUtil.randomString(32));
        toolsCheckPlan.setGmtCreate(DateUtil.date());
        toolsCheckPlan.setCreateUser(ShiroUtils.getUserId());
        toolsCheckPlan.setDelFlag(Constant.IS_NOT_DEL_INT);
        return toolsCheckPlanDao.save(toolsCheckPlan);
    }

    /**
     * @param toolsCheckPlan 实体
     * @return
     */
    @Override
    public int update(ToolsCheckPlanEntity toolsCheckPlan) {
        toolsCheckPlan.setGmtModified(DateUtil.date());
        toolsCheckPlan.setModifyUser(ShiroUtils.getUserId());
        // 处理下校验计划起止时间：
        Date startDate = toolsCheckPlan.getStartDate();
        if (startDate != null) {
            toolsCheckPlan.setStartDate(DateUtil.beginOfDay(startDate));
        }
        // 设置结束日期
        Date endDate = toolsCheckPlan.getEndDate();
        if (endDate != null) {
            toolsCheckPlan.setEndDate(DateUtil.endOfDay(endDate));
        }
        return toolsCheckPlanDao.update(toolsCheckPlan);
    }

    /**
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return toolsCheckPlanDao.delete(id);
    }

    /**
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return toolsCheckPlanDao.deleteBatch(ids);
    }

    @Override
    public Map<String, Object> export(String planId) {
        Map<String, Object> result = MapUtil.newHashMap();
        List<ToolsCheckPlanImplements> planImplements = toolsCheckTicketDao.exportImplements(planId);
        result.put("list", Optional.ofNullable(planImplements).orElse(CollectionUtil.newArrayList()));
        return result;
    }
}
